package actions.admintool;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;

import com.opensymphony.xwork2.ActionSupport;

import dao.BarRepository;
import domain.Bar;

@Namespace("/")
@ResultPath(value="/")
@Action(value="CRUDBar")
public class CRUDBarAction extends ActionSupport{
	
	public static final long serialVersionUID = 1L;
	private BarRepository br = new BarRepository();
	
	private Bar bar;
	private String id;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	
	@Action(value="editBar", results={
			@Result(name="success",location="pages/admintool/edit_bar.jsp")
	})
	public String execute() {		
		try {	
			bar = br.getById(Long.parseLong(id));			
		} catch (Exception e) {			
			bar = new Bar(); 
		}
		return SUCCESS;
	}
	
	@Action(value="removeBar", results={
			@Result(name="success",type="redirect",location="")
	})
	public String remove() {	
		
		if (bar == null) {
			bar = br.getById(Long.parseLong(id));
		}
		br.delete(bar);
		return SUCCESS;
	}
	
	@Action(value="saveBar", results={
			@Result(name="success",location="pages/admintool/bar_detail.jsp"),
			@Result(name="error",location="pages/admintool/bar_detail_error.jsp")
	})
	public String save() {				try {
			
			if (bar.getId() != null) {
				br.update(bar);
			} else {
				br.insert(bar);
			}
			
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}			
	}
	
	public Bar getBar() {
		return bar;
	}

	public void setBar(Bar bar) {
		this.bar = bar;
	}	

}
